package cn.mxz.robots.province;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 宁夏
 * 排名:完成
 * 去年排名：完成
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-19
 */
public class Ningxia extends Exam implements IRank
{
	public static void main(String[] args)
	{
		Student student = new Student("15640102530228", "101110", "130825199708030020", "234", BEN_KE_LI_KE);
		Ningxia gansu = new Ningxia(student);
		// System.out.println(gansu.getCaptcha());
		// System.out.println("请输入验证码：");
		// @SuppressWarnings("resource")
		// String captcha = new Scanner(System.in).next();
		// System.out.println(gansu.login(captcha));

		gansu.lastYearRank();
	}

	public String getCaptcha()
	{
		delCookie();
		Request captchaRequest = new Request("http://61.133.219.10/exam/admin/AuthCode.do?k=randExam", mSha1);
		return curlFile(captchaRequest);
	}

	public Ningxia(Student student)
	{
		super(student);
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://61.133.219.10/exam/common/LoginExam.do", mSha1);
		loginRequest.setCharSet("gbk");
		loginRequest.addHeader("Referer", "http://61.133.219.10/exam/common/LoginExam.do");
		loginRequest.addParams("name", mStudent.getStudentId());
		loginRequest.addParams("password", mStudent.getStudentPwd());
		loginRequest.addParams("authcode", captcha);
		loginRequest.addParams("type", "");
		loginRequest.addParams("x", "135");
		loginRequest.addParams("y", "15");
		String loginHtml = curlPost(loginRequest);
		JSONObject resultObject = new JSONObject();

		if (loginHtml.contains("考生信息"))
		{
			Request logRequest = new Request("http://61.133.219.10/exam/common/Score.do", mSha1);
			loginHtml = curlPost(logRequest).replace("&nbsp;", "");
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
			Request logoutRequest = new Request("http://61.133.219.10/exam/common/LoginExam_logout.do", mSha1);
			logoutRequest.addHeader("Referer", "http://61.133.219.10/exam/common/Student_getStudentInfo.do");
			curlPost(logoutRequest);
		}
		else if (loginHtml.contains("用户名或密码错误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_USER_PASSWORD);
		}
		else if (loginHtml.contains("验证码错误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_CAPTCHA);
		}
		else
		{
			System.out.println(loginHtml);
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document loginDocument = Jsoup.parse(parseHtml);
		JSONObject result = new JSONObject();

		Element table = loginDocument.select("table").get(4);
		Element tr = table.select("table").get(0).select("tr").get(2);
		result.put("exam_id", mStudent.getStudentId());
		String nameString = table.select("div").get(0).text();
		result.put("name", regexMatch(nameString, "生：(.*)考生"));
		result.put("ranking",
				getRank(31 + 20, regexMatch(tr.select("td").get(7).text(), "(\\d+)\\."), mStudent.getType()));
		result.put("province", "宁夏回族自治区");
		// Element tr = table.select("table").get(0).select("tr").get(2);
		JSONObject scoreObject = new JSONObject();
		scoreObject.put("语文", tr.select("td").get(1).text());
		scoreObject.put("数学", tr.select("td").get(2).text());
		scoreObject.put("外语", tr.select("td").get(3).text());
		scoreObject.put("综合", tr.select("td").get(4).text());
		scoreObject.put("外语口语", tr.select("td").get(5).text());
		scoreObject.put("总分", regexMatch(tr.select("td").get(7).text(), "(\\d+)\\."));
		scoreObject.put("最终分数", tr.select("td").get(7).text());

		result.put("score", scoreObject.toString());
		return result.toString();
	}

	@Override
	public void initRank()
	{
		// TODO Auto-generated method stub
		GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
		GaoKaoProvince province = provinceService.get(31 + 20);
		// GaoKaoRankService rankService = new GaoKaoRankService();

		// 理科排名
		// String rankHtml = curlGet(new
		// Request("http://www.nxjyks.cn/contents/ZHXX/2015/06/20150623102229000.html"));
		// 文科排名
		String rankHtml = curlGet(new Request("http://www.nxjyks.cn/contents/ZHXX/2015/06/20150623102256000.html"));
		Document rankDom = Jsoup.parse(rankHtml);
		Elements as = rankDom.select("table").get(2).select("tr");
		int asSize = as.size();
		Integer preInteger = 0;
		for (int j = 0; j < 8; j = j + 2)
		{
			for (int i = 2; i < asSize; i++)
			{
				Elements tds = as.get(i).select("td");

				String scoreString = regexMatch(tds.get(j).text(), "(\\d+)");
				String leijiString = tds.get(j + 1).text();
				if (leijiString.equals("　"))
					continue;
				Integer score = Integer.parseInt(scoreString);
				Integer ranking = preInteger + 1;
				preInteger = Integer.parseInt(leijiString);

				GaoKaoRank rank = new GaoKaoRank();

				rank.setScore(score);
				rank.setType(BEN_KE_WEN_KE);
				rank.setGaoKaoProvince(province);
				rank.setRanking(ranking);

				// rankService.save(rank);

				System.out.println(rank);
				System.out.println("score: " + scoreString + "rank: " + ranking);

			}
		}
	}

	@Override
	public void lastYearRank()
	{
		GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
		GaoKaoProvince province = provinceService.get(20);
		GaoKaoRankService rankService = new GaoKaoRankService();

		// 理科 http://www.gxeduw.com/news/2014/106761.html
		String rankHtml = curlGet(new Request("http://www.gxeduw.com/news/2014/106762.html"));
		// 文科 http://www.gxeduw.com/news/2014/106762.html
		Document rankDom = Jsoup.parse(rankHtml);
		Elements as = rankDom.select("table").get(0).select("tr");
		Integer pre = 0;
		int asSize = as.size();
		for (int i = 2; i < asSize; i++)
		{
			String scoreString = regexMatch(as.get(i).select("td").get(0).text(), "(\\d+)");
			// String countString =
			// regexMatch(as.get(i).select("td").get(1).text(), "(\\d+)");
			String leiji = regexMatch(as.get(i).select("td").get(1).text(), "(\\d+)");

			if (scoreString.equals("") || leiji.equals(""))
				continue;
			Integer ranking = pre + 1;

			Integer count = Integer.parseInt(leiji) - pre;
			pre = Integer.parseInt(leiji);

			Integer score = Integer.parseInt(scoreString);
			GaoKaoRank rank = new GaoKaoRank();

			rank.setScore(score);
			rank.setCount(count);
			rank.setType(BEN_KE_WEN_KE);
			rank.setGaoKaoProvince(province);
			rank.setRanking(ranking);

			rankService.save(rank);
			System.out.println("score: " + scoreString + "count: " + count + "rank: " + ranking);
			System.out.println(rank);
		}
	}
}
